import type { GlobalThemeOverrides } from 'naive-ui'
import { computed, watch } from 'vue'
import { darkTheme, useOsTheme } from 'naive-ui'
import { useAppStore } from '@/store'

export function useTheme() {
  const appStore = useAppStore()

  const OsTheme = useOsTheme()

  const isDark = computed(() => {
    if (appStore.theme === 'auto')
      return OsTheme.value === 'dark'
    else
      return appStore.theme === 'dark'
  })

  const theme = computed(() => {
    return isDark.value ? darkTheme : undefined
  })

  const themeOverrides = computed<GlobalThemeOverrides>(() => {
    console.log(isDark.value)
    if (isDark.value) {
      return {
        common: {},
      }
    }
    return {
      common: {
        "primaryColor": "#484aa1",
        "primaryColorHover": "#5756af",
        "primaryColorPressed": "#41439a",
        "primaryColorSuppl": "#5756af",
        "infoColor": "#2080f0",
        "infoColorHover": "#4098fc",
        "infoColorPressed": "#1060c9",
        "infoColorSuppl": "#4098fc",
        "successColor": "#18a058",
        "successColorHover": "#5756af",
        "successColorPressed": "#41439a",
        "successColorSuppl": "#5756af",
        "warningColor": "#f0a020",
        "warningColorHover": "#fcb040",
        "warningColorPressed": "#c97c10",
        "warningColorSuppl": "#fcb040",
        "errorColor": "#d03050",
        "errorColorHover": "#de576d",
        "errorColorPressed": "#ab1f3f",
        "errorColorSuppl": "#de576d"
      }

    }
  })

  watch(
    () => isDark.value,
    (dark) => {
      if (dark)
        document.documentElement.classList.add('dark')
      else
        document.documentElement.classList.remove('dark')
    },
    { immediate: true },
  )
  console.log(themeOverrides)

  return { theme, themeOverrides }
}
